package com.example.demo.TakeMaterial.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.TakeMaterial.dto.AddGood;
import com.example.demo.TakeMaterial.dto.RequisitionEditInfo;
import com.example.demo.TakeMaterial.entity.MaterialRequisition;
import com.example.demo.TakeMaterial.entity.RequisitionHeader;
import com.example.demo.TakeMaterial.mapper.MaterialRequisitionMapper;
import com.example.demo.TakeMaterial.mapper.RequisitionHeaderMapper;
import com.example.demo.TakeMaterial.service.MaterialRequisitionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.common.ResponseData;
import com.example.demo.entryWarehouse.entity.AcceptGoods;
import com.example.demo.entryWarehouse.entity.AcceptInform;
import com.example.demo.entryWarehouse.mapper.AcceptGoodsMapper;
import com.example.demo.entryWarehouse.mapper.AcceptInformMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangxuan
 * @since 2021-05-17
 */
@Service
public class MaterialRequisitionServiceImpl extends ServiceImpl<MaterialRequisitionMapper, MaterialRequisition> implements MaterialRequisitionService {

    @Autowired
    MaterialRequisitionMapper materialRequisitionMapper;

    @Autowired
    RequisitionHeaderMapper requisitionHeaderMapper;

    @Autowired
    private AcceptGoodsMapper acceptGoodsMapper;

    @Autowired
    private AcceptInformMapper acceptInformMapper;

    @Override
    public ResponseData selectAll() {
        List<MaterialRequisition> materialRequisition = materialRequisitionMapper.selectList(null);
        return ResponseData.buildOk(materialRequisition);
    }

    @Override
    public ResponseData selectById(String id) {
        List<MaterialRequisition> acceptGoodsList = new ArrayList<>();
        QueryWrapper<MaterialRequisition> wrapper = new QueryWrapper<>();
        wrapper.eq("requisition_id",id);
        acceptGoodsList = materialRequisitionMapper.selectList(wrapper);
        return ResponseData.buildOk(acceptGoodsList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData insertGoods(AddGood goods) {

        QueryWrapper<RequisitionHeader> wrapper2 = new QueryWrapper<>();

        wrapper2.eq("requisition_id",goods.getRequisitionId());

        RequisitionHeader res = requisitionHeaderMapper.selectOne(wrapper2);
        String AccID = res.getAcceptId();
        QueryWrapper<AcceptGoods> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("accept_id",AccID)
                .eq("name",goods.getMaterialName())
                .eq("specification",goods.getSpecification())
                .eq("unit",goods.getUnit());

        AcceptGoods acceptGoods =  acceptGoodsMapper.selectOne(wrapper3);

        int _NumGoods = acceptGoods.getYesNum();

        /*QueryWrapper<MaterialRequisition> wrapper1 = new QueryWrapper<>();

        wrapper1.eq("requisition_id",goods.getRequisitionId())
                .eq("name",goods.getMaterialName())
                .eq("specification",goods.getSpecification())
                .eq("unit",goods.getUnit());

        MaterialRequisition materialRequisition2 = materialRequisitionMapper.selectOne(wrapper1);

        int x = materialRequisition2.getDueNum();
        System.out.println("已申请的数量  " +x);
        int _NumGoods = acceptGoods.getYesNum();

        System.out.println("库存的数量  " +_NumGoods);


        if(_NumGoods < (goods.getPurchaseNum()+x)){
            return ResponseData.buildError("超出最大申请数量");
        }




         */

        MaterialRequisition materialRequisition = new MaterialRequisition();

        materialRequisition.setTax(acceptGoods.getTax());
        materialRequisition.setName(goods.getMaterialName());
        materialRequisition.setDueNum(goods.getPurchaseNum());
        //materialRequisition.setReamrk(goods.getRemark());
        materialRequisition.setRequisitionId(goods.getRequisitionId());
        materialRequisition.setSpecification(goods.getSpecification());
        materialRequisition.setUnit(goods.getUnit());
        materialRequisition.setPrice(goods.getPrice());

        //materialRequisition.setId();
        QueryWrapper<MaterialRequisition> wrapper = new QueryWrapper<>();

        Integer Num = materialRequisitionMapper.selectCount(wrapper);
        System.out.println(Num);
        materialRequisition.setId(Num+1);
        wrapper.eq("requisition_id",goods.getRequisitionId())
                .eq("name",goods.getMaterialName())
                .eq("specification",goods.getSpecification())
                .eq("unit",goods.getUnit());

        MaterialRequisition materialRequisition1 = materialRequisitionMapper.selectOne(wrapper);
        //没有添加过此物料
        if(materialRequisition1==null){
            if(_NumGoods < goods.getPurchaseNum()){
                return ResponseData.buildError("超出最大申请数量");
            }

            int i = materialRequisitionMapper.insert(materialRequisition);
            if (i > 0) return ResponseData.buildOk();
            return ResponseData.buildError();
        }
        else
        {
            int n = materialRequisition1.getDueNum() + goods.getPurchaseNum();
            if(_NumGoods < n){
                return ResponseData.buildError("超出最大申请数量");
            }
            //int num = materialRequisition1.getYesNum() + 1;
            UpdateWrapper<MaterialRequisition> wrapper5 = new UpdateWrapper<>();
            wrapper5.eq("requisition_id",materialRequisition.getRequisitionId())
                    .eq("name",materialRequisition.getName())
                    .eq("specification",materialRequisition.getSpecification())
                    .eq("unit",materialRequisition.getUnit())
                    .set("due_num",n);
            int j = materialRequisitionMapper.update(null,wrapper5);
            if (j > 0) return ResponseData.buildOk();
            return ResponseData.buildError();
        }

    }

    @Override
    public ResponseData editGood(MaterialRequisition materialRequisition) {
        //此处注意，需要测试,主要是bigdemical转json！！！undo
        //只更新账单所列数量、*单价、*税率、合格数量、不合格数量、存放位置、备注
        int i = materialRequisitionMapper.updateById(materialRequisition);
        if (i > 0) {
            System.out.println("成功插入");
            return ResponseData.buildOk();
        }
        return ResponseData.buildError();
    }

    @Override
    public ResponseData RemoveGood(String id) {

        MaterialRequisition materialRequisition = materialRequisitionMapper.selectById(id);
        QueryWrapper<RequisitionHeader> wrapper = new QueryWrapper<>();
        wrapper.eq("requisition_id",materialRequisition.getRequisitionId());
        RequisitionHeader requisitionHeader = requisitionHeaderMapper.selectOne(wrapper);
        if (requisitionHeader.getStatus().equals("申请中")){
            int i = materialRequisitionMapper.deleteById(id);
            if (i > 0) return ResponseData.buildOk();
            return ResponseData.buildError();
        }else {
            return ResponseData.buildError();
        }
    }

    @Override
    public ResponseData Select_AccId(String acceptId) {
        List<AcceptGoods> entryFormList = new ArrayList<>();
        QueryWrapper<AcceptGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("accept_id",acceptId);
        entryFormList = acceptGoodsMapper.selectList(wrapper);
        return ResponseData.buildOk(entryFormList);
    }

    @Override
    public ResponseData Select(String department, String provider) {
        List<AcceptInform> acceptInforms = new ArrayList<>();
        QueryWrapper<AcceptInform> wrapper = new QueryWrapper<>();
        wrapper.eq("department",department)
                .eq("provider",provider)
                .eq("status","验收完成");
        acceptInforms = acceptInformMapper.selectList(wrapper);
        return ResponseData.buildOk(acceptInforms);
    }

    @Override
    public ResponseData editMater(MaterialRequisition materialRequisition) {
        //此处注意，需要测试,主要是bigdemical转json！！！undo
        //只更新账单所列数量、*单价、*税率、合格数量、不合格数量、存放位置、备注
        int i = materialRequisitionMapper.updateById(materialRequisition);
        if (i > 0) {
            System.out.println("成功插入");
            return ResponseData.buildOk();
        }
        return ResponseData.buildError();
    }

    @Override
    public ResponseData removeMater(String id) {

        MaterialRequisition materialRequisition = materialRequisitionMapper.selectById(id);
        QueryWrapper<RequisitionHeader> wrapper = new QueryWrapper<>();
        int i = materialRequisitionMapper.deleteById(id);
        if (i > 0) return ResponseData.buildOk();
        else return ResponseData.buildError();
    }




    @Override
    public ResponseData selectByHeaderStatus(String status) {
        QueryWrapper<MaterialRequisition> queryWrapper = new QueryWrapper<>();
        queryWrapper.exists("(select * from requisition_header where " +
                "requisition_header.`status` = '" + status + "' and requisition_header.requisition_id" +
                " = material_requisition.requisition_id)");
        List<MaterialRequisition> materialRequisitionList = list(queryWrapper);
        return ResponseData.buildOk(materialRequisitionList.size(), materialRequisitionList);
    }

    @Override
    public ResponseData deliveryEdit(RequisitionEditInfo updForm) {
        UpdateWrapper<MaterialRequisition> wrapper = new UpdateWrapper<>();
        wrapper.set("actual_num", updForm.getActualNum()).set("reamrk", updForm.getReamrk()).eq("id", updForm.getId());
        if (update(wrapper)) {
            return ResponseData.buildOk("修改成功");
        }
        return ResponseData.buildError();
    }
}
